package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.result.Result;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    /**
     * 工作台今日数据查询
     *
     * @return
     */

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public BusinessDataVO getBusinessData(Date begin, Date end) {

        /*
         * 1.营业额：当日已完成订单的总金额
         * 2.有效订单数：当日已完成状态的订单数量
         * 3.订单完成率：有效订单数/总订单数
         * 4.平均客单价：营业额/有效订单数
         * 5.新增用户数：当日注册的用户数量
         */

        HashMap map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);

        //1.查询总订单数(当日所有状态的订单数)
        Integer totalOrderCount = ordersMapper.getOrderCount(map);
        //2.查询营业额(当日已完成状态的订单的总金额)
        map.put("status", Orders.COMPLETED);//已完成状态
        Double turnover = ordersMapper.sumByMap(map);
        turnover = turnover == null ? 0 : turnover;
        //3.查询有效订单数(当日已完成状态的订单数)
        Integer validOrderCount = ordersMapper.getOrderCount(map);

        //4.订单完成率(有效订单数/总订单数）
        Double orderCompletionRate = 0.0;
        Double unitPrice = 0.0;
        if (totalOrderCount != 0 && validOrderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;

            //5.平均客单价(营业额/有效订单数)
            unitPrice = turnover / validOrderCount;
        }

        //6.新增用户数(当日注册的用户数量)
        Integer newUsers = userMapper.getUserCount(map);


        return BusinessDataVO.builder().turnover(turnover).validOrderCount(validOrderCount).orderCompletionRate(orderCompletionRate).unitPrice(unitPrice).newUsers(newUsers).build();
    }

    @Override
    public OrderOverViewVO getOrderOverView() {
        /**
         * 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
         */
        LocalDateTime now = LocalDateTime.now();
        // 当天开始时间：00:00:00
        Date begin = Date.from(now.truncatedTo(ChronoUnit.DAYS)//截断到“天”级别，保留年月日，时分秒设为0
                .atZone(ZoneId.systemDefault())//// 关联系统默认时区（如Asia/Shanghai）
                .toInstant());/// 转为UTC时间戳（Instant）
        HashMap map = new HashMap();
        map.put("begin", begin);
        map.put("status", Orders.TO_BE_CONFIRMED);//待接单状态
        //1.查询待接单的订单数
        Integer waitingOrders = ordersMapper.getOrderCount(map);
        //2.查询待派送的订单数
        map.put("status", Orders.DELIVERY_IN_PROGRESS);//派送中状态
        Integer deliveredOrders = ordersMapper.getOrderCount(map);
        //3. 查询已完成的订单数
        map.put("status", Orders.COMPLETED);//已完成状态
        Integer completedOrders = ordersMapper.getOrderCount(map);
        //4.查询已取消的订单数
        map.put("status", Orders.CANCELLED);//已取消状态
        Integer cancelledOrders = ordersMapper.getOrderCount(map);

        //5.查询全部订单数
        map.put("status", null);
        Integer allOrders = ordersMapper.getOrderCount(map);


        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders).build();
    }

    @Override
    public DishOverViewVO getDishOverView() {
        HashMap map = new HashMap();
        map.put("status", StatusConstant.ENABLE);//启用状态
        //1.查询已启售的菜品数量
        Integer sold = dishMapper.countByMap(map);
        map.put("status", StatusConstant.DISABLE);//禁用状态
        //2.查询已停售的菜品数量
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();

    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        HashMap map = new HashMap();
        map.put("status", StatusConstant.ENABLE);//启用状态
        //1.查询已启售的菜品数量
        Integer sold = setmealMapper.countByMap(map);
        map.put("status", StatusConstant.DISABLE);//禁用状态
        //2.查询已停售的菜品数量
        Integer discontinued = setmealMapper.countByMap(map);
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }


}
